﻿/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2010>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using MyPhotoIndex.Interfaces.Plugins;
using System.Collections.ObjectModel;
using MyPhotoIndex.Interfaces;
using MyPhotoIndex.Plugins.Utilities.Export.Properties;
using System.Windows.Forms;
using MyPhotoIndex.Interfaces.Imaging;
using System.IO;
using MyPhotoIndex.Interfaces.Tagging;

namespace MyPhotoIndex.Plugins.Utilities.Export
{
    public class FindMissingFilesPlugin : IUtilityPlugin
    {
        private IApplicationContext m_applicationContext;

        #region IUtilityPlugin Members

        public void ImageSelectionChanged(ReadOnlyCollection<int> selectedImages, SelectedImageInfo lastSelected)
        {

        }

        public void RegisterMenues(List<IPluginMenuInfo> pluginMenuInfoList)
        {
            PluginMenuInfo menuInfo = new PluginMenuInfo();
            menuInfo.MenuItemId = Guid.NewGuid();
            menuInfo.Name = "Export";
            menuInfo.Icon = Resources.nav_up_right_blue;
            menuInfo.MenuItemClick += new EventHandler<PluginMenuClickEventArgs>(menuInfo_MenuItemClick);

            pluginMenuInfoList.Add(menuInfo);
        }

        void menuInfo_MenuItemClick(object sender, PluginMenuClickEventArgs e)
        {
            SaveFileDialog fd = new SaveFileDialog();
            fd.Filter = "Comma delimited files (*.csv) | *.csv";
            DialogResult dr = fd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(fd.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                using (StreamWriter sw = new StreamWriter(fs, Encoding.Unicode))
                {
                    fs.SetLength(0);
                    foreach (IImageData imageData in m_applicationContext.ImageCollection)
                    {
                        sw.Write("\"");
                        sw.Write(imageData.Id.ToString());
                        sw.Write("\"");
                        sw.Write(",");
                        sw.Write("\"");
                        sw.Write(imageData.FileName);
                        sw.Write("\"");
                        sw.Write(",");
                        int i = 0;
                        sw.Write("\"");
                        foreach (int tagId in imageData.Tags)
                        {
                            IImageTag tag = m_applicationContext.ImageTagCollection.GetTag(tagId);
                            if (tag != null)
                            {
                                sw.Write(tag.Name);
                            }
                            i++;

                            if (i < imageData.Tags.Length && tag != null)
                            {
                                sw.Write("|");
                            }
                        }
                        sw.Write("\"");

                        sw.Write(Environment.NewLine);
                    }
                }
            }
        }

        #endregion

        #region IPlugin Members

        public string Description
        {
            get { return "Export Data"; }
        }

        public bool Load(MyPhotoIndex.Interfaces.IApplicationContext applicationContext)
        {
            m_applicationContext = applicationContext;

            return true;
        }

        public string Name
        {
            get { return "Export Data"; }
        }

        public void Unload()
        {

        }

        #endregion
    }
}
